import { Application } from "egg";

export default (app: Application) => {
  const { STRING, INTEGER, DATE, TEXT } = app.Sequelize;

  const ProjectUpdate = app.model.define(
    "ProjectUpdate",
    {
      id: {
        type: INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      projectId: {
        type: INTEGER,
        allowNull: false,
        comment: "项目ID",
      },
      title: {
        type: STRING(200),
        allowNull: false,
        comment: "进展标题",
      },
      content: {
        type: TEXT,
        allowNull: false,
        comment: "进展内容",
      },
      images: {
        type: TEXT,
        allowNull: true,
        comment: "进展图片，JSON格式",
      },
      authorId: {
        type: INTEGER,
        allowNull: false,
        comment: "发布者ID",
      },
    },
    {
      tableName: "project_updates",
      timestamps: true,
      paranoid: true,
      underscored: true,
    }
  );

  // 关联关系
  ProjectUpdate.associate = function (): void {
    // 进展报告属于一个项目
    app.model.ProjectUpdate.belongsTo(app.model.Project, {
      foreignKey: "projectId",
      as: "project",
    });

    // 进展报告属于一个作者
    app.model.ProjectUpdate.belongsTo(app.model.User, {
      foreignKey: "authorId",
      as: "author",
    });
  };

  return ProjectUpdate;
};
